from django.db import models
from django.core.exceptions import ValidationError
from django.core.validators import MaxValueValidator, MinValueValidator

# Create your models here.
class PMS5003S(models.Model):
    # 单位：浓度单位 微克/立方米，数量单位 超过直径微米的个数。甲醛 克/立方米。
    # 注：工业环境金属颗粒，大气环境下普通室内。
    # 校验位，传感器已计算校验，返回数据100%正确，不需记录。
    class ErrorMessage(models.TextChoices):
        OFFLINE = 'offline', '离线'
        NODATA = 'nodata', '没一点数据'
        NOT_FOUND_START_BIT = 'not_found_start_bit', '未找到数据起始位'
        # NOT_COLLECTED_ALL_DATA = 'not_collected_all_data', '没收集够数据位'
        # NOT_PASSED_VALIDATE = 'not_passed_validate', '没通过校验'

    pm1dot0_industry = models.FloatField(verbose_name='pm1.0工业环境', default=0.0)
    pm2dot5_industry = models.FloatField(verbose_name='pm2.5工业环境', default=0.0)
    pm10dot0_industry = models.FloatField(verbose_name='pm10.0工业环境', default=0.0)
    pm1dot0_atmosphere = models.FloatField(verbose_name='pm1.0大气环境', default=0.0)
    pm2dot5_atmosphere = models.FloatField(verbose_name='pm2.5大气环境', default=0.0)
    pm10dot0_atmosphere = models.FloatField(verbose_name='pm10.0大气环境', default=0.0)
    pm0dot3_quantity = models.IntegerField(verbose_name='pm0.3数量', default=0)
    pm0dot5_quantity = models.IntegerField(verbose_name='pm0.5数量', default=0)
    pm1dot0_quantity = models.IntegerField(verbose_name='pm1.0数量', default=0)
    pm2dot5_quantity = models.IntegerField(verbose_name='pm2.5数量', default=0)
    pm5dot0_quantity = models.IntegerField(verbose_name='pm5.0数量', default=0)
    pm10dot0_quantity = models.IntegerField(verbose_name='pm10.0数量', default=0)
    formaldehyde = models.FloatField(verbose_name='甲醛', default=0.0)

    create_time = models.DateTimeField(verbose_name='创建时间', auto_now=True)
    state = models.BooleanField(verbose_name='状态', default=False)
    error_message = models.CharField(choices=ErrorMessage.choices, max_length=50, default=None, null=True)

    class Meta:
        verbose_name = '攀藤PMS5003s粉尘甲醛传感器'


class DHTXX(models.Model):
    # DHT11/DHT22温湿度传感器
    # 数据相同共用一张表。精度不同。驱动步骤相同，但注意时序不同，驱动代码有所不同。
    class ErrorMessage(models.TextChoices):
        OFFLINE = 'OFFLINE', '离线'
        UPPER_HANDSHAKE_PULL_DOWN = 'UPPER_HANDSHAKE_PULL_DOWN', '上位机下拉电平开始握手信号'
        UPPER_HANDSHAKE_PULL_UP = 'UPPER_HANDSHAKE_PULL_UP', '上位机释放总线,自动上拉，等待应答'
        LOWER_HANDSHAKE_PULL_DOWN = 'LOWER_HANDSHAKE_PULL_DOWN', '传感器握手下拉'
        LOWER_HANDSHAKE_PULL_UP = 'LOWER_HANDSHAKE_PULL_UP', '传感器握手上拉'
        DATA_PULL_DOWN = 'DATA_PULL_DOWN', '传感器开始传输数据'
        NOT_COLLECTED_ALL_DATA = 'NOT_COLLECTED_ALL_DATA', '没收集够数据位'
        NOT_PASSED_VALIDATE = 'NOT_PASSED_VALIDATE', '没通过校验'

    class DHTModel(models.TextChoices):
        DHT11 = 'DHT11', 'DHT11'
        DHT22 = 'DHT22', 'DHT22/AM2302'

    temperature = models.FloatField(verbose_name='温度', default=0.0, validators=[MinValueValidator(-20), MaxValueValidator(60)])
    humidity = models.FloatField(verbose_name='湿度', default=0.0, validators=[MinValueValidator(5), MaxValueValidator(95)])
    model = models.CharField(verbose_name='具体型号', choices=DHTModel.choices, max_length=20, null=True)

    create_time = models.DateTimeField(verbose_name='创建时间', auto_now=True)
    state = models.BooleanField(verbose_name='状态', default=False)
    error_message = models.CharField(choices=ErrorMessage.choices, max_length=50, default=None, null=True)

    class Meta:
        verbose_name = '奥松DHT11/DHT22温湿度传感器'